<?php
/*
 * @Date: 2021-01-29 13:37:42
 * @名称: 
 * @版本: 0.01
 * @作者: 初雪桜
 * @邮箱: 202184199@qq.com
 * @最后编辑: 初雪桜
 * @LastEditTime: 2021-01-29 14:29:42
 * @FilePath: /Site/app/useradmin/controller/SelectController/SelectUserInfo/SelectUseInfor.php
 */

namespace app\useradmin\controller\SelectController\SelectUserInfo;

use app\BaseController;
use think\facade\Db;

class SelectUseInfor extends BaseController
{
    public function SelectUseInfor($token, $keyWord, $postData)
    {
        if (strlen($keyWord) >= 7) { //查询条件为7位起步
            $return_data = Db::table('user_info')
                ->where(
                    "user_name",
                    'like',
                    "%$keyWord%"
                )
                ->alias('user_info')
                ->join('author_plan author_plan', 'author_plan.user_id = user_info._id')
                ->field([
                    'user_info.user_name',
                    'user_info._id',
                ])
                ->select();
            if (count($return_data) > 5) { //判断预选用户有几个
                return errorJsonReturn("Select-User-10001");
            } else { //返回用户数据
                $hashHmac_toKen = hash_hmac("sha256", $token .  $postData['bookId'] . $postData['time'], config("useradminConfig.useradmin_Edit_EditComicsInfo_sha256Encryption")); //加密后 加密id
                $hashHmac_bookId = hash_hmac("sha256",   $postData['bookId'] . $postData['time'], config("useradminConfig.useradmin_Edit_EditComicsInfo_sha256Encryption")); //加密后 加密id
                if ($hashHmac_toKen == $postData['toKen']) { //判断加密的token是不是通过
                    if ($hashHmac_bookId == $postData['verificationBookId']) { //验证书籍id
                        $user_list = [];
                        $date =  date("Y-m-d H:i:s");
                        foreach ($return_data as $key => $value) {
                            $user_id = [

                                "hash_hmac_user_id" => hash_hmac('sha256', $value["_id"] . $postData['bookId'] . $date, config("useradminConfig.useradmin_Edit_EditComicsInfo_sha256Encryption")),
                                "user_id" => $value["_id"],
                                "time" => $date,
                            ];
                            $user_list[$key] = [
                                "name" => $value["user_name"],
                                "value" => authcode(base64_encode(json_encode($user_id)), 'encode', config("useradminConfig.useradmin_Edit_EditComicsInfo_sha256Encryption")),
                                "selected" => false,
                                "disabled" => false,
                            ];
                        }
                        return json_decode(successJsonReturn($user_list)->getContent(), true);
                    } else {
                        return errorJsonReturn('Comics-10003'); //到这里就是验证错误 验证书籍错误
                    }
                } else {
                    return errorJsonReturn('Token-10002', 'SeverError'); //到这里就是验证错误 验证书籍错误
                }
            }
        } else {
            return errorJsonReturn('Select-User-10000'); //搜索关键字长度不够

        }
    }
}
